/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-10-06 21:08:50
 * Last Modified: 2017-05-02 17:43:09
 * File Name: controllers/washroom.go
 * Description:
 */
package controllers

import (
	"encoding/json"

	"qkleague/errorcode"
	rpc "qkleague/rpc/client/qingke"
	//"qkleague/statis"

	log "github.com/cihub/seelog"
)

type WashroomController struct {
	LoginedController
}

/*
	查询洗衣房列表
*/
// @router /list [get]
func (p *WashroomController) ListWashroom() {
	defer p.Output("ListWashroom")

	if p.Userid == testUid {
		data := getTestUserInfoConf("../conf/test_user_info/washroom-list.conf")
		tmp := data.(map[string]interface{})
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = tmp["data"]
		return
	}

	if list := rpc.WashRoomList(p.Userid); list != nil {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["data"] = list
	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
	}
}

/*
	添加洗衣房
*/
// @router /add [post]
func (p *WashroomController) AddWashroom() {
	defer p.Output("AddWashroom")

	if p.Userid == testUid {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["room_id"] = "test13000000000"
		return
	}

	body := p.Ctx.Input.RequestBody
	log.Infof("recv body:%s", string(body))
	req := make(map[string]interface{})
	if err := json.Unmarshal(body, &req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	req["userid"] = p.Userid

	if ok, roomId, msg := rpc.AddWashRoom(p.Userid, req); ok {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		p.Resp["room_id"] = roomId

	} else {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = msg
	}
}

//删除洗衣房
//@router /delete [post]
func (p *WashroomController) DelWashroom() {
	defer p.Output("DelWashroom")

	if p.Userid == testUid {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		return
	}

	body := p.Ctx.Input.RequestBody
	req := make(map[string]interface{})
	if err := json.Unmarshal(body, &req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := rpc.DelWashRoom(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}

//修改洗衣房信息
//@router /modify [post]
func (p *WashroomController) ModifyWashroom() {
	defer p.Output("ModifyWashroom")

	if p.Userid == testUid {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		return
	}

	body := p.Ctx.Input.RequestBody
	log.Infof("recv body:%s", string(body))
	req := make(map[string]interface{})
	if err := json.Unmarshal(body, &req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := rpc.ModifyWashRoom(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}
